package com.lbj.googleplay.googleplay100.ui.holder;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lbj.googleplay.googleplay100.R;
import com.lbj.googleplay.googleplay100.http.HttpHelper;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import org.xutils.ImageManager;
import org.xutils.x;

import java.util.ArrayList;

public class HomeHeaderHolder extends BaseHolder<ArrayList<String>> {

    private ViewPager mViewPager;
    private ArrayList<String> data;
    private LinearLayout container;
    private int mPreviousPoint;//记录上一个点的位置
    @Override
    public View initView() {
        //创建根布局,相对布局
        RelativeLayout rlRoot = new RelativeLayout(UIUtils.getContext());
        //初始化布局参数
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,UIUtils.dip2px(150));
        rlRoot.setLayoutParams(layoutParams);
        //添加viewpager
         mViewPager = new ViewPager(UIUtils.getContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
        rlRoot.addView(mViewPager,params);//将viewpager添加给相对布局
        //初始化指示器,线性布局
         container = new LinearLayout(UIUtils.getContext());
        container.setOrientation(LinearLayout.HORIZONTAL);
        RelativeLayout.LayoutParams llParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置内边距
        int padding = UIUtils.dip2px(10);
        container.setPadding(padding,padding,padding,padding);

        //设置展示规则
        llParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        llParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        //添加到根布局
        rlRoot.addView(container,llParams);

        return rlRoot;
    }

    @Override
    public void refreshView(final ArrayList<String> data) {
        this.data = data;
        //填充viewpager数据
        mViewPager.setAdapter(new HomeHeaderAdapter());
        //设置当前显示那一页
        mViewPager.setCurrentItem(data.size()*1000);
        //初始化指示器
        for (int i = 0; i <data.size() ; i++) {
            ImageView point = new ImageView(UIUtils.getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i==0){//第一个默认选中
                point.setImageResource(R.drawable.indicator_selected);

            }else {
                point.setImageResource(R.drawable.indicator_normal);
                params.leftMargin = UIUtils.dip2px(6);

            }
            point.setLayoutParams(params);
            container.addView(point);
        }
        //设置viewpager监听事件
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                position = position%data.size();
                //当前点被选中
                ImageView point = (ImageView) container.getChildAt(position);
                point.setImageResource(R.drawable.indicator_selected);
                //上一个点变为不选中
                ImageView previousPoint = (ImageView) container.getChildAt(mPreviousPoint);
                previousPoint.setImageResource(R.drawable.indicator_normal);
                
                mPreviousPoint = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //实现循环滚动
        HomeHeaderTask task = new HomeHeaderTask();
        task.start();
    }

    class HomeHeaderTask implements Runnable{

        public void start(){
            //移除所有的消息
            UIUtils.getHandler().removeCallbacksAndMessages(null);
            //发送延时消息
            UIUtils.getHandler().postDelayed(this,1000);
        }
        @Override
        public void run() {
            int currentItem = mViewPager.getCurrentItem();
            currentItem++;
            mViewPager.setCurrentItem(currentItem);
            //继续发消息,实现内循环
            UIUtils.getHandler().postDelayed(this,1000);

        }
    }
    class HomeHeaderAdapter extends PagerAdapter{

        private ImageManager imageManager;

        public HomeHeaderAdapter() {
            imageManager = x.image();
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            position = position%data.size();
            String url = data.get(position);
            ImageView view = new ImageView(UIUtils.getContext());
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            imageManager.bind(view, HttpHelper.URL+"image?name="+url);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }

}
